﻿/* -------------------------------------------------------------
 * 公司:iSun 软件工作室
 * 用才江怀玉
 * 
 * 
 * 
 * 
 *
 * 
 * 
 * 
*/


using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.SessionState;
using iSun.Common;

using iSun.Model;

namespace iSun.Web.UI.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public class EntityJsonData : JsonData, IHttpHandler, IRequiresSessionState 
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }
                                                         
        public  void ProcessRequest(HttpContext context)
        {
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");            //对应的实体
            CreateBussniess(strEntity);
            string strCondition = this.GetStringFromQueryString("condition", "");            //系统特定条件
            string strCustomCondition = this.GetStringFromQueryString("customCondition", "");//自定义条件
            int iPageIndex = this.GetIntFromQueryString("pageindex", 0);                     //第N页？
            int iPageSize = this.GetIntFromQueryString("pagesize", 0);                       //页记录数？
            string IsOnlyTotal = this.GetStringFromQueryString("onlyTotal", "false");         //是否只回传记录总数?
            string IsDesc = this.GetStringFromQueryString("desc", "false");                  //Yes or No
            string IsSetCache = this.GetStringFromQueryString("setcache", "false");
            string IsRefreshCache = this.GetStringFromQueryString("IsRefreshCache", "false");
            // strCondition = Server.UrlDecode(strCondition);
            // strCustomCondition = Server.UrlDecode(strCustomCondition);
            // 系统数据权限
            //strCondition = strCondition == "" ? getRoleCondition() : strCondition + (getRoleCondition() == "" ? "" : " and " + getRoleCondition());
            strCondition = strCondition == "" ? strCustomCondition : strCondition + (strCustomCondition == "" ? "" : " and " + strCustomCondition);


            string strType = this.GetStringFromQueryString("type", "");
            string strOrder = this.GetStringFromQueryString("order", "");
            strEntity = strEntity.Split(',')[0];

            //记录数
            int totalrecord = GetTotal(strEntity, strCondition);

            if (IsOnlyTotal == "true")
            {
               context.Response.Write("[{\"total\":" + totalrecord.ToString() + "}]");
                return;
            }

            if (IsSetCache == "true")
            {
                if (IsRefreshCache == "false")
                {
                    SetCache(strEntity, strCondition, totalrecord, iPageSize, strOrder);
                }
                else
                {
                    RefreshCache(strEntity, strCondition, totalrecord, iPageSize, strOrder);
                }
                return;
            }
            //if(ZX.Common.
            if ((GetCondtionCache(strEntity) == strCondition) && !string.IsNullOrEmpty(GetDataCache(strEntity, iPageIndex)))
            {
                context.Response.Write(GetDataCache(strEntity, iPageIndex));
                return;
            }

            IList<BaseEntity> list;
            list = GetData(strEntity, iPageIndex, iPageSize, strOrder, strCondition);

            if (list == null)
            {
                context.Response.Write("{\"total\":" + totalrecord.ToString() + ",\"items\":[]}");
                return;
            }
            //int totalrecord = bll.GetRowCount(strCondition);
            //string strFormat = "{\"total\":" + totalrecord.ToString() + ",\"rows\":[";

            //strFormat = strFormat + str + "]}";
            //strFormat = strFormat.Replace(new Regex("(^|[^\\\\])\\\"\\\\/Date\\((-?[0-9]+)\\)\\\\/\\\"","$1new Date($2)");
            if (strType == "1")
            {
                string str = list.ToJSON().Replace("\n", " ").Replace("\r", " ").Replace("null", "").Replace("[BR]", " ").Replace("@@@@", " ").Replace("@@", " ");
                context.Response.Write("[" + str.Replace("null", "").Replace("[A]", "&") + "]");
            }
            else
            {
                context.Response.Write(GetListJson(list, totalrecord));
            }
        }
        #endregion


    }
}
